package com.aaa.power.service.impl;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.power.entity.Role;
import com.aaa.power.mapper.RoleMapper;
import com.aaa.power.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author YuXin
 * @since 2020-11-10 13:52:39
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param roleId 主键
     * @return 实例对象
     */
    @Override
    public Role queryById(Integer roleId) {
        return this.roleMapper.queryById(roleId);
    }


    /**
     * 查询指定行数据
     *
     * @param pageNo   查询起始位置
     * @param pageSize 查询条数
     * @param role     查询条数
     * @return Result ---- Result.getData() 对象列表
     */
    @Override
    public Result queryAllByParam(Integer pageNo, Integer pageSize, Role role) {
        if (pageNo == 0 || pageSize == 0) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<Role> pageInfo = new PageInfo<>(roleMapper.queryAll(role));
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), pageInfo);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param role 实例对象
     * @return 对象列表
     */
    @Override
    public List<Role> queryAll(Role role) {
        return this.roleMapper.queryAll(role);
    }

    /**
     * 新增数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @Override
    public Result insert(Role role) {
        this.roleMapper.insert(role);
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), role);

    }

    /**
     * 修改数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @Override
    public Role update(Role role) {
        this.roleMapper.update(role);
        return this.queryById(role.getRoleId());
    }

    /**
     * 通过主键删除数据
     *
     * @param roleId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer roleId) {
        return this.roleMapper.deleteById(roleId) > 0;
    }

    @Override
    public int addRoleAndMenu(int roleId, String menuIds) {
        // 删除原有角色关联的所有权限
        int r1 = roleMapper.deleteMenuByRoleId(roleId);
        System.out.println("获取到的菜单ID：" + menuIds);
        int r2 = 0;
        //分割menuIds  1，2,3,100,101,103
        if (!StringUtils.isEmpty(menuIds)) {
            //roleIdsAndMenuIds = (1,1),(1,2),(1,3),(1,100),....
            StringBuffer roleIdsAndMenuIds = new StringBuffer();
            String[] midArray = menuIds.split(",");
            //循环遍历
            for (String menuId : midArray) {
                roleIdsAndMenuIds.append("(" + roleId + "," + menuId + "),");
            }
            //去掉最后一个逗号
            String tmpStr = roleIdsAndMenuIds.substring(0, roleIdsAndMenuIds.length() - 1);
            //执行添加
            r2 = roleMapper.addRoleAndMenu(tmpStr);
        }
        //上面两个操作都成功，返回1
        if (r1 > 0 && r2 > 0) {
            return 1;
        }
        return 0;
    }
}